package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.PriAlloc;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IPriAllocService;
import com.huitone.gddw.service.dto.PriAllocDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信优先级分配 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="priAlloc")
public class PriAllocController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(PriAllocController.class);
	
	@Autowired
	private IPriAllocService priAllocService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<PriAllocDTO> query(QueryPage<PriAlloc> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询短信优先级分配";
		Map<String, Object> params = new HashMap<String, Object>();
		String ruleId = request.getParameter("ruleId");
		String svrNode = request.getParameter("svrNode");
		String serviceCode = request.getParameter("serviceCode");
		String isLevel = request.getParameter("isLevel");
		params.put("ruleId", ruleId);
		params.put("svrNode", svrNode);
		params.put("serviceCode", serviceCode);
		params.put("isLevel", isLevel);
		page.setParams(params);
		QueryPage<PriAllocDTO> result = null;
		try {
			result = priAllocService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询附加条件规则信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 修改页面数据准备
	 * @param recId 记录ID
	 * @return PriAllocDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public PriAllocDTO toUpdate(String recId) {
		try {
			PriAllocDTO entity = priAllocService.toUpdate(recId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new PriAllocDTO();
		}
	}
	
	/**
	 * 更新
	 * @param entity PriAlloc
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(PriAlloc entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改短信优先级分配";
		boolean isSuccess = true;
		String msg;
		try {
			PriAlloc priAlloc = priAllocService.selectByPrimaryKey(entity.getRecId());
			priAlloc.setSvrNode(entity.getSvrNode());
			priAlloc.setServiceCode(entity.getServiceCode());
			priAlloc.setRuleId(entity.getRuleId());
			priAlloc.setDesc(entity.getDesc());
			priAllocService.update(priAlloc);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改短信优先级分配信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
}
